<template>
  <div class="amap-page-container" style="width: 80%; height:500px;margin:30px 10%;">
    <el-amap
      ref="map"
      id="container"
      vid="amapDemo"
      :zoom="12"
      class="amap-demo"
    >
    </el-amap>
  </div>
</template>

<script>
import {AMapManager} from "vue-amap";

export default {
  name: "index",
  data() {
    const self = this;
    return {};
  },
  methods: {
    position() {
      var map = new AMap.Map("container", {resizeEnable: true});//初始化地图
      console.log(this.lnglats)
      //信息窗口实例
      var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
      //遍历生成多个标记点
      for (var i = 0; i < this.lnglats.length; i++) {
        var marker = new AMap.Marker({
          position: [this.lnglats.lng,this.lnglats.lat],//不同标记点的经纬度
          map: map
        });
        marker.content = "标记点["+i+"]";
        marker.on('click', markerClick);
        marker.emit('click', {target: marker});//默认初始化不出现信息窗体,打开初始化就出现信息窗体
      }

      function markerClick(e) {
        infoWindow.setContent(e.target.content);
        infoWindow.open(map, e.target.getPosition());
      }

      map.setFitView();
    }

  },
  mounted() {
    this.position();
  },
  props: {
    lnglats: {
      type: Array,
      default:function(){
        return [];
      }
    }
  },
  watch: {}
}
</script>

<style scoped>
.search-box {
  position: absolute;
  top: 25px;
  left: 20px;
}

.amap-page-container {
  position: relative;
}
</style>
